package org.zky.modules.merchant.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zky.modules.merchant.domain.ZkyCourseTab;
import org.zky.modules.merchant.repository.ZkyCourseTabRepository;
import org.zky.modules.merchant.service.ZkyCourseTabService;
import org.zky.modules.merchant.service.dto.ZkyCourseTabDto;
import org.zky.modules.merchant.service.dto.ZkyCourseTabQueryCriteria;
import org.zky.modules.merchant.service.mapper.ZkyCourseTabMapper;
import org.zky.utils.FileUtil;
import org.zky.utils.PageUtil;
import org.zky.utils.QueryHelp;
import org.zky.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author chenglong
* @date 2020-03-23
*/
@Service
//@CacheConfig(cacheNames = "zkyCourseTab")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ZkyCourseTabServiceImpl implements ZkyCourseTabService {

    private final ZkyCourseTabRepository zkyCourseTabRepository;

    private final ZkyCourseTabMapper zkyCourseTabMapper;

    public ZkyCourseTabServiceImpl(ZkyCourseTabRepository zkyCourseTabRepository, ZkyCourseTabMapper zkyCourseTabMapper) {
        this.zkyCourseTabRepository = zkyCourseTabRepository;
        this.zkyCourseTabMapper = zkyCourseTabMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ZkyCourseTabQueryCriteria criteria, Pageable pageable){
        Page<ZkyCourseTab> page = zkyCourseTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zkyCourseTabMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ZkyCourseTabDto> queryAll(ZkyCourseTabQueryCriteria criteria){
        return zkyCourseTabMapper.toDto(zkyCourseTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ZkyCourseTabDto findById(Long courseId) {
        ZkyCourseTab zkyCourseTab = zkyCourseTabRepository.findById(courseId).orElseGet(ZkyCourseTab::new);
        ValidationUtil.isNull(zkyCourseTab.getCourseId(),"ZkyCourseTab","courseId",courseId);
        return zkyCourseTabMapper.toDto(zkyCourseTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ZkyCourseTabDto create(ZkyCourseTab resources) {
        return zkyCourseTabMapper.toDto(zkyCourseTabRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ZkyCourseTab resources) {
        ZkyCourseTab zkyCourseTab = zkyCourseTabRepository.findById(resources.getCourseId()).orElseGet(ZkyCourseTab::new);
        ValidationUtil.isNull( zkyCourseTab.getCourseId(),"ZkyCourseTab","id",resources.getCourseId());
        zkyCourseTab.copy(resources);
        zkyCourseTabRepository.save(zkyCourseTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long courseId : ids) {
            zkyCourseTabRepository.deleteById(courseId);
        }
    }

    @Override
    public void download(List<ZkyCourseTabDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZkyCourseTabDto zkyCourseTab : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" courseName",  zkyCourseTab.getCourseName());
            map.put("pictures", zkyCourseTab.getPicture());
            map.put("campus contact", zkyCourseTab.getContent());
            map.put(" remark",  zkyCourseTab.getRemark());
            map.put(" address",  zkyCourseTab.getAddress());
            map.put("amount * 100", zkyCourseTab.getAmount());
            map.put("1:active 2:inactive", zkyCourseTab.getStatus());
            map.put("delete time", zkyCourseTab.getDeleteAt());
            map.put(" createTime",  zkyCourseTab.getCreateTime());
            map.put(" updateTime",  zkyCourseTab.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}